package com.sanuo.pico.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.sanuo.pico.R;
import com.sanuo.pico.databinding.WarnItemBinding;
import com.sanuo.pico.event.Event;

public class WarnAdapter extends RecyclerView.Adapter {

    final String[] ss;
    final LayoutInflater inflater;

    public WarnAdapter(Context c) {
        inflater = LayoutInflater.from(c);
        ss = c.getResources().getStringArray(R.array.warn);
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        WarnItemBinding binding = WarnItemBinding.inflate(inflater, parent, false);
        return new Holder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof Holder) {
            ((Holder) holder).bind(position);
        }
    }

    @Override
    public int getItemCount() {
        return ss.length;
    }

    class Holder extends RecyclerView.ViewHolder {

        WarnItemBinding binding;

        Holder(WarnItemBinding binding) {
            super(binding.getRoot());
            this.binding = binding;
        }

        void bind(int p) {
            binding.tv.setText(ss[p]);
            Integer i = Event.warn.getValue()[p];
            if (i != null && i == 1) {
                binding.img.setImageResource(R.mipmap.check_wrong);
            } else {
                binding.img.setImageResource(R.mipmap.check_ok);
            }
        }
    }
}
